//
//  Demo1ViewController.m
//  DLSlideViewDemo
//
//  Created by Dongle Su on 14-12-11.
//  Copyright (c) 2014年 dongle. All rights reserved.
//

#import "YiQiViewController.h"
#import "DLFixedTabbarView.h"
#import "JianJieViewController.h"
#import "ZhaoPinViewController.h"


#import "ChengZhangViewController.h"

@interface YiQiViewController ()<UIAlertViewDelegate>
{
UIButton * collectionBtn;

}
@end

@implementation YiQiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
      [self addRightItem];
    
    //请求数据 （只判断是否一点击收藏）
    [U76HONet companyHomePageWithAction:@"" code:_yqCode ucode:[Singleton share].code dates:@"" andDataBlock:^(CompanyDetailModel *model) {
        NSLog(@"公司首页获取数据成功了");
        if ([model.baseinfomodel.isok isEqualToString:@"1"] || [model.baseinfomodel.isok isEqualToString:@"2"]) {
            collectionBtn.selected = YES;
        }
        
    }];

    

    // Do any additional setup after loading the view from its nib.
//    self.tabedSlideView.viewControllers = @[ctrl1, ctrl2, ctrl3];
    
    self.HeaderImageView.layer.masksToBounds = YES;
    self.HeaderImageView.layer.cornerRadius = 31;
    
//    self.HeaderImageView.layer.borderWidth = 5;
//    
//    self.HeaderImageView.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor grayColor]);
    
    [self.HeaderImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,self.headerString]] placeholderImage:[UIImage imageNamed:@"发现－默认logo.png"]];
    
    
   
    self.tabedSlideView.baseViewController = self;
    self.tabedSlideView.tabItemNormalColor = [UIColor blackColor];
    
    //self.tabedSlideView.backgroundColor = [UIColor redColor];
    self.tabedSlideView.tabItemSelectedColor = [UIColor colorWithRed:0.66 green:0.80 blue:0.19 alpha:1.00];
    self.tabedSlideView.tabbarTrackColor = [UIColor colorWithRed:0.66 green:0.80 blue:0.19 alpha:1.00];
    //self.tabedSlideView.tabbarBackgroundImage = [UIImage imageNamed:@"tabbarBk"];
    self.tabedSlideView.tabbarBottomSpacing = 3.0;
    
    DLTabedbarItem *item1 = [DLTabedbarItem itemWithTitle:@"成长" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
    DLTabedbarItem *item2 = [DLTabedbarItem itemWithTitle:@"简介" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
    DLTabedbarItem *item3 = [DLTabedbarItem itemWithTitle:@"招聘" image:[UIImage imageNamed:@""] selectedImage:[UIImage imageNamed:@""]];
    self.tabedSlideView.tabbarItems = @[item1, item2, item3];
    [self.tabedSlideView buildTabbar];
    
    self.tabedSlideView.selectedIndex = 0;


}

-(void)addRightItem
{
    collectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    collectionBtn.frame = CGRectMake(0, 0, 36, 34);
    [collectionBtn setImage:[UIImage imageNamed:@"收藏－图标"] forState:UIControlStateNormal];
    [collectionBtn setImage:[UIImage imageNamed:@"选中－收藏图标"] forState:UIControlStateSelected];
    collectionBtn.tag = 504;
    [collectionBtn addTarget:self action:@selector(collectionAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:collectionBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

#pragma mark - btn的点击事件们
-(void)collectionAction:(UIButton *)sender
{
    if (sender.selected == NO) {
        if ([self isLogin]) {
            [self addCollection];
        }
        
    }
    //    if (tip == 0) {
    //        [Singleton alerMessage:@"收藏成功" img:@"cancle.png"];
    //        tip = 1;
    //    }
    //    else
    //    {
    //        [Singleton alerMessage:@"取消成功" img:@"cancle.png"];
    //        tip = 0;
    //    }
}
-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
                AppDelegate * app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                app.window.rootViewController = nvc;
                
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
        AppDelegate * app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        app.window.rootViewController = nvc;
    }
}
-(void)addCollection
{
    [U76HONet addCollectionOrfriendsWithAction:@"" code:[Singleton share].code code1:_yqCode andDataBlock:^(NSDictionary *dic) {
        NSLog(@"收藏成功了");
        collectionBtn.selected = YES;
        
        [Singleton alerMessage:@"收藏成功" img:@"已收藏"];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfTabsInDLTabedSlideView:(DLTabedSlideView *)sender{
    return 3;
}
- (UIViewController *)DLTabedSlideView:(DLTabedSlideView *)sender controllerAt:(NSInteger)index{
    
    
    switch (index) {
        case 0:
        {
            
          
            
            ChengZhangViewController *ctrl = [[ChengZhangViewController alloc] init];
           ctrl.yqCode = self.yqCode;
            

            
            
            return ctrl;
        }
        case 1:
        {
            
            JianJieViewController *ctrl = [[JianJieViewController alloc] init];
            ctrl.yqCode = self.yqCode;


                       // ctrl.view.backgroundColor = [UIColor redColor];
            return ctrl;
        }
        case 2:
        {
            ZhaoPinViewController *ctrl = [[ZhaoPinViewController alloc] init];
            ctrl.yqCode = self.yqCode;
            return ctrl;
        }

        default:
            return nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
